package com.sdry.web.controller.tdd;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sdry.entity.tdd.Reg;
import com.sdry.service.tdd.RegService;
import com.sdry.utils.DateJsonValueProcessor;
import com.sdry.utils.ResponseUtil;
import com.sdry.web.controller.BaseController;
/**
 * 
 * @ClassName:regController
 * @Description:地区
 * @Author tdd
 * @Date 2018年4月25日下午5:05:43
 * @version 1.0
 */
@Controller
@RequestMapping("/reg")
public class RegController extends BaseController{
	
	@Resource RegService  regService;
	
	/**
	 * 条件分页查询
	 * @param reg
	 * @param request
	 * @param session
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public String list(Reg reg,HttpServletRequest request,HttpSession session, HttpServletResponse response) throws Exception{
		/*
		 * 条件分页查询内容
		 */
		List<Reg> list = regService.findPageByMution(reg);
		/*
		 * 条件查询总条数
		 */
		Integer total = regService.selectCountByMution(reg);  
		
		/*
		 * 转为json数据传至页面
		 */
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd hh:mm:ss"));
		//创建result
		JSONObject result = new JSONObject();
		//转json数据
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		//添加入result
		result.put("rows", jsonArray);
		result.put("total", total);
		//发送result
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 * 添加地区
	 * @param reg
	 * @param request
	 * @param session
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/add")
	public String add(Reg reg,HttpServletRequest request,HttpSession session, HttpServletResponse response) throws Exception{
		/*
		 * 添加返回id
		 */
		Long rid = regService.add(reg);
		if (null != rid) {
			response.getWriter().print("1");
		}else{
			response.getWriter().print("0");
		}
		return null;
	}
	/**
	 * 根据id查地区
	 * @param rid
	 * @param request
	 * @param session
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryById")
	public Reg queryById(Long rid,HttpServletRequest request,HttpSession session, HttpServletResponse response) throws Exception{
		Reg reg = regService.queryById(rid);
		return reg;
	}
	
	/**
	 * 条件查询
	 * @param job
	 * @param request
	 * @param session
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryAll")
	public List<Reg> queryAllByMution(Reg reg,HttpServletRequest request,HttpSession session, HttpServletResponse response) throws Exception{
		List<Reg> list = regService.queryAllByMution(reg);
		return list;
	}
	
	/**
	 * 修改地区
	 * @param reg
	 * @param request
	 * @param session
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/update")
	public String update(Reg reg,HttpServletRequest request,HttpSession session, HttpServletResponse response) throws Exception{
		/*
		 * 修改返回影响行数
		 */
		Integer count = regService.update(reg);
		if (count >= 1) {
			response.getWriter().print("1");
		}else{
			response.getWriter().print("0");
		}
		return null;
	}
	/**
	 * 多删除
	 * @param ids
	 * @param request
	 * @param session
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	public String delete(Serializable[] ids,HttpServletRequest request,HttpSession session, HttpServletResponse response) throws Exception{
		/*
		 * 多删除返回影响行数
		 */
		Integer count = regService.delete(ids);
		if (count >= 1) {
			response.getWriter().print("1");
		}else{
			response.getWriter().print("0");
		}
		return null;
	}
}
